/*
 * Copyright (c) 2018-2999 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.mall.api.controller;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.context.TokenUtils;
import com.lam.framework.operLog.annotation.Log;
import com.lam.mall.app.param.ProdCommParam;
import com.lam.mall.app.vo.ProdCommVo;
import com.lam.mall.entity.ProdComm;
import com.lam.mall.service.ProdCommService;

/**
 */
@RestController
@RequestMapping("/${frontPath}/prodComm")
@Log(title = "评论接口")
public class ProdApiCommController extends BaseController{
	
	@Autowired
    private ProdCommService prodCommService;

    @GetMapping("/prodCommData")
    @Log(oper = "返回商品评论数据(好评率 好评数量 中评数 差评数)")
    public Result getProdCommData(String prodId) {
        return Result.ok(prodCommService.getByProdId(prodId));
    }

    @GetMapping("/prodCommPageByUser")
    @Log(oper = "根据用户返回评论分页数据")
    public Result getProdCommPage() {
    	startPage();
    	List<ProdCommVo> list = prodCommService.getPageByUserId(TokenUtils.getLoginUserid());
        return Result.ok(getTableData(list));
    }

    @GetMapping("/prodCommPageByProd")
    @Log(oper = "根据商品返回评论分页数据")
    public Result getProdCommPageByProdId(String prodId, Integer evaluate) {
        startPage();
    	List<ProdCommVo> list = prodCommService.getPageByProdId(prodId, evaluate);
        return Result.ok(getTableData(list));
    }

    @PostMapping
    @Log(oper = "添加评论")
    public Result saveProdCommPage(ProdCommParam prodCommParam) {
        ProdComm prodComm = new ProdComm();
        prodComm.setProdId(prodCommParam.getProdId());
        prodComm.setOrderItemId(prodCommParam.getOrderItemId());
        prodComm.setUserId(TokenUtils.getLoginUserid());
        prodComm.setScore(prodCommParam.getScore());
        prodComm.setContent(prodCommParam.getContent());
        prodComm.setPics(prodCommParam.getPics());
        prodComm.setIsAnonymous(prodCommParam.getIsAnonymous());
        prodComm.setCreateTime(new Date());
        prodComm.setStatus(0);
        prodComm.setEvaluate(prodCommParam.getEvaluate());
        prodCommService.insert(prodComm);
        return Result.ok();
    }

    @DeleteMapping
    @Log(oper = "删除评论" )
    public Result deleteProdComm(String prodCommId) {
        prodCommService.deleteById(prodCommId);
        return Result.ok();
    }
}
